/*
 * Copyright (c) 2018-2021. data2viz sàrl.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */

package com.gitee.wsl.common.struct


import com.gitee.wsl.common.struct.sankey.SankeyLayout
import com.gitee.wsl.test.shouldBeClose
import kotlin.test.Test

class SankeyTests {

    val width = 960.0f
    val height = 500.0f

    val data = listOf(0, 1, 2, 3, 4)
    val matrix = listOf(
        listOf(0, 0, 2, 0, 2),
        listOf(0, 0, 2, 2, 0),
        listOf(0, 0, 0, 2, 2),
        listOf(0, 0, 0, 0, 4),
        listOf(0, 0, 0, 0, 0)
    )

    val dataX0 = listOf(.0f, .0f, 315.0f, 630.0f, 945.0f)
    val dataX1 = listOf(15.0f, 15.0f, 330.0f, 645.0f, 960.0f)
    val dataY0 = listOf(.0f, 255.0f, 128.03282f, 176.65376f, .0f)
    val dataY1 = listOf(245.0f, 500.0f, 373.0328f, 421.65375f, 490.0f)

    fun fromTo(from:Int, to:Int):Float = matrix[from][to].toFloat()


    @Test
    fun testSankey() {
        val sankeyLayout = SankeyLayout<Int>()
        sankeyLayout.nodeWidth = 15.0f
        sankeyLayout.nodePadding = 10.0f
        sankeyLayout.width = width
        sankeyLayout.height = height

        val sankey = sankeyLayout.sankey(data, ::fromTo)
        data.forEachIndexed { index, _ ->
            sankey.nodes[index].x0 shouldBeClose dataX0[index]
            sankey.nodes[index].x1 shouldBeClose dataX1[index]
            sankey.nodes[index].y0 shouldBeClose dataY0[index]
            sankey.nodes[index].y1 shouldBeClose dataY1[index]
        }
    }
}
